﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CommandButtonDefinition.h"
#include "CommandControlDefinition.h"
#include "GbmpNew.h"
#include "ControlDefinitionChangeEvent.h"
#include "GbmpFileSystem.h"
#include "FilePath.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

CommandButtonDefinition::CommandButtonDefinition(const std::wstring& id, const std::wstring& caption, const std::wstring& tooltip, const std::wstring& commandId, const std::wstring& iconPath, ButtonStyle buttonStyle)
    : m_pControlDefinition(NEW_AS_OWNER_PTR(CommandControlDefinition, id, caption, tooltip, commandId))
    , m_buttonStyle(buttonStyle)
{
    m_pDefinitionChangeEvent = NEW_AS_OWNER_PTR(ControlDefinitionChangeEvent, L"CommandButtonDefineChangeEvent", this);
    m_iconPath = iconPath == L"" ? iconPath : FilePath::GetAbsolutePath(iconPath, gcmp::FileSystem::GetExeDirPath());
    
}

CommandButtonDefinition::~CommandButtonDefinition()
{
}

bool gcmp::CommandButtonDefinition::SetIconPath(const std::wstring& iconPath)
{
    if (m_iconPath != iconPath)
    {
        m_iconPath = iconPath;
        ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Other);
        m_pDefinitionChangeEvent->Emit(&arguments);
    }
    return true;
}

bool gcmp::CommandButtonDefinition::SetButtonStyle(ButtonStyle style)
{
    if (m_buttonStyle != style)
    {
        m_buttonStyle = style;
        ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Other);
        m_pDefinitionChangeEvent->Emit(&arguments);
    }
    return true;
}

bool gcmp::CommandButtonDefinition::SetCaption(const std::wstring& caption)
{
    if (m_pControlDefinition->GetCaption() != caption)
    {
        m_pControlDefinition->SetCaption(caption);
        ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Other);
        m_pDefinitionChangeEvent->Emit(&arguments);
    }
    return true;
}

void gcmp::CommandButtonDefinition::SetIsChecked(bool isChecked)
{
    if (m_pControlDefinition->IsChecked() != isChecked)
    {
        m_pControlDefinition->SetIsChecked(isChecked);
        ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Other);
        m_pDefinitionChangeEvent->Emit(&arguments);
    }
}

gcmp::IControlDefinitionChangeEvent* gcmp::CommandButtonDefinition::GetControlDefinitionChangeEvent() const
{
    return const_cast<ControlDefinitionChangeEvent*>(m_pDefinitionChangeEvent.get());
}
